/********页面操作*******/
var global = {};//全局对象，共享数据
global.host = "http://123.206.18.189:8080/";//设置主机

var question = new Question();
question.setPaperId(sessionStorage.getItem("examId"));
var qEditor = new QuestionEditor(question,"#timu","#answers-list");
//初始化题目
// qEditor.initTitle();

// 选择题型
$("#question_type_group button.btn").click(function(){
    $("#question_type_group button.btn").removeClass("btn-primary");
    $(this).addClass("btn-primary");
    var type = $(this).attr("attr_value");
    console.info(question);
    question.setType(type);
    qEditor.resetQuestion();//清空选项
    qEditor.initOption(3);//默认初始化5个选项
});
//模拟点击事件，初始化为单选类型
$("#single_select_btn").click();
/**
 * 添加选项
 */
$("#add-answer").click(function(){
    qEditor.addOption();
});

/**
 * 添加试题成功后，回调函数
 * @param msg
 */
function dealSubmitQuestion(xhr,msg) {
    if(xhr.status == 200) {
        alert("添加试题成功");
        location.reload();
    } else {
        alert("添加试题失败");
        location.reload();
    }
}
function dealChangeQuestion(xhr,msg){
    if(xhr.status == 200 && xhr.readyState == 4){
        alert("试题修改成功！")
    }else{
        alert("试题修改失败！")
    }
}
//提交页面数据
$("#question_submit_btn").click(function(){
    console.log(2);
    let question = qEditor.question;
    var questionId = qEditor.titleId;
    var title = qEditor.getTextValue(questionId);
    if(qEditor.isEmpty(title)){
        return alert("题目不能为空！");
    }
    //设置题目
    question.setTitle(title);
    var options = question.options;
    var optionNumber = 0;
    for(let key in options) {
        var option = options[key];
        var opValue = qEditor.getTextValue("#"+ option.id);
        if(qEditor.isEmpty(opValue)) {
            option.setContent("");
        } else {
            option.setContent(opValue);
            optionNumber++;
        }

        var score = $("#" +option.scoreId).val().trim();
        var extra_info = $("#extra_info").val().trim();
        question.setInfo(extra_info);
        if(score.length > 0){
            if(/^[0-9]*$/.test(score)) {
                option.setScore(Number(score));
            } else {
                alert("分数必须是数字！");
                return ;
            }
        }
    }

    if(optionNumber < 1) {
        if(!confirm("确定该实体没有选项吗？")){
            return false;
        }
    }

    var pureQuestion = qEditor.getPureQuestion();
    var url = global.host + "question";
    postJson(url,pureQuestion,dealSubmitQuestion);
    // console.info(pureQuestion);
});

//修改提交页面
$("#question_change_btn").click(function(){
    let question = qEditor.question;
    var questionId = qEditor.titleId;
    var title = qEditor.getTextValue(questionId);
    if(qEditor.isEmpty(title)){
        return alert("题目不能为空！");
    }
    //设置题目
    question.setTitle(title);
    var options = question.options;
    var optionNumber = 0;
    for(let key in options) {
        var option = options[key];
        var opValue = qEditor.getTextValue("#"+ option.id);
        if(qEditor.isEmpty(opValue)) {
            option.setContent("");
        } else {
            option.setContent(opValue);
            optionNumber++;
        }
        var score = $("#" +option.scoreId).val().trim();
        if(score.length > 0){
            if(/^[0-9]*$/.test(score)) {
                option.setScore(Number(score));
            } else {
                alert("分数必须是数字！");
                return ;
            }
        }
    }

    if(optionNumber < 1) {
        if(!confirm("确定该实体没有选项吗？")){
            return false;
        }
    }
    var pureQuestion = qEditor.getPureQuestion();
    console.info(pureQuestion);
    var url = global.host + "question";
    putJson(url,pureQuestion,dealChangeQuestion);
    // console.info(pureQuestion);
});
/**
 * 提交数据
 * @param url  请求地址
 * @param postData  提交的数据
 * @param method  提交的方式
 * @param callback 回调函数，用于处理后继请求
 */
function postJson(url,postData,callback) {
    $.ajax({
        type: "POST",
        url: url,
        headers:{
          authToken:sessionStorage.getItem("token")
        },
        contentType: "application/json; charset=utf-8",
        data: JSON.stringify(postData),
        complete: function(xhr,msg){
            callback(xhr,msg);
        }
    });
}
function putJson(url,putData,callback) {
    $.ajax({
        type: "PUT",
        url: url,
        headers:{
            authToken:sessionStorage.getItem("token")
        },
        contentType: "application/json; charset=utf-8",
        data: JSON.stringify(putData),
        complete: function(xhr,msg){
            callback(xhr,msg);
        }
    });
}